package com.example.wlzz.service.serviceImpl;
import com.example.wlzz.constant.RedisConstant;
import com.example.wlzz.dto.OpenUserInfoDTO;
import com.example.wlzz.dto.UserInfoDTO;
import com.example.wlzz.entity.UserInfo;
import com.example.wlzz.entity.User_;
import com.example.wlzz.enums.ResultEnum;
import com.example.wlzz.repository.UserInfoRepository;
import com.example.wlzz.repository.UserRepository;
import com.example.wlzz.service.UserInfoService;
import com.example.wlzz.utils.ResultVOUtil;
import com.example.wlzz.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UserInfoServiceImpl implements UserInfoService{
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public ResultVO alterImgUrl(String imgUrl, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setImgUrl(imgUrl);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public ResultVO useridGetForInfo(int userid) {
            UserInfo userInfo =  userInfoRepository.findByUserId(userid);
            if(userInfo==null){
                return ResultVOUtil.error(ResultEnum.USERINFO_NO_EXIST.getCode(),ResultEnum.USERINFO_NO_EXIST.getMessage());
            }
            OpenUserInfoDTO openUserInfoDTO = new OpenUserInfoDTO();
            BeanUtils.copyProperties(userInfo,openUserInfoDTO);
            if(userInfo.getBirth()!=null){
                Long getTime = userInfo.getBirth().getTime();
                openUserInfoDTO.setBirth(getTime.toString());
            }
            return ResultVOUtil.success(openUserInfoDTO);
      }

    @Override
    public ResultVO getUserInfo(String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfo,userInfoDTO);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
             if(userInfo.getBirth()!=null){
                 userInfoDTO.setBirth(simpleDateFormat.format(userInfo.getBirth()));
             }
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }
    @Override
    public ResultVO alterNickName(String nickName, String token) {
       String phoneToken = toLoginForToken(token);
       if(!"fail".equals(phoneToken)){
         User_ user =  userRepository.findByPhone(phoneToken);
         UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
         userInfo.setNickName(nickName);
         UserInfoDTO userInfoDTO = new UserInfoDTO();
         BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
         return ResultVOUtil.success(userInfoDTO);
       }else{
          return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
       }
    }

    @Override
    public ResultVO alterRealName(String realName, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setRealName(realName);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public UserInfo findByUserId(Integer userId) {
        return userInfoRepository.findByUserId(userId);
    }

    @Override
    public ResultVO alterSex(String sex, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setSex(sex);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }
    @Override
    public ResultVO alterBirth(String birth, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date birth_ =  simpleDateFormat.parse(birth);
                userInfo.setBirth(birth_);
                UserInfoDTO userInfoDTO = new UserInfoDTO();
                BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
                userInfoDTO.setBirth(simpleDateFormat.format(birth_));
                return ResultVOUtil.success(userInfoDTO);
            }catch(Exception e){
                return ResultVOUtil.error(ResultEnum.ALTER_BIRTH_ERROR.getCode(),ResultEnum.ALTER_BIRTH_ERROR.getMessage());
            }
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public ResultVO alterMotto(String motto, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setMotto(motto);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public ResultVO alterSchoolName(String schoolName, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setSchoolName(schoolName);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public ResultVO alterDepartmentName(String departmentName, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setDepartmentName(departmentName);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }
    @Override
    public ResultVO alterMajorName(String majorName, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setMajorName(majorName);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public ResultVO alterSno(String sno, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setSno(sno);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }

    @Override
    public ResultVO alterSnoPassword(String snoPassword, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setSnoPassword(snoPassword);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfoRepository.save(userInfo),userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }
    public ResultVO alterSnoAndSnoPassword(String sno,String snoPassword, String token) {
        String phoneToken = toLoginForToken(token);
        if(!"fail".equals(phoneToken)){
            User_ user =  userRepository.findByPhone(phoneToken);
            UserInfo userInfo =  userInfoRepository.findByUserId(user.getUserId());
            userInfo.setSnoPassword(snoPassword);
            userInfo.setSno(sno);
            UserInfo userInfo1 =   userInfoRepository.save(userInfo);
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfo1,userInfoDTO);
            return ResultVOUtil.success(userInfoDTO);
        }else{
            return ResultVOUtil.error(ResultEnum.NO_LOGIN.getCode(),ResultEnum.NO_LOGIN.getMessage());
        }
    }
    public String toLoginForToken(String token){   //判断是否在登陆状态
        //去redis查询
        String phoneToken = redisTemplate.opsForValue().get(String.format(RedisConstant.LOGIN_SUCCESS_TOKEN, token));
        if (!StringUtils.isEmpty(phoneToken)) {  //查询redis
            return phoneToken;
        }
        else{
            return "fail";
        }
    }
}
